package com.tanhua.dubbo.api.Impl;

import com.itheima.dubbo.api.CommentsApi;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongoDb.Comment;
import com.tanhua.model.mongoDb.Movement;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

/**
 * @Author Pan Rundong
 * @Date 2023-03-02 21:14
 * @Desc 评论Api实现类
 */
@DubboService
public class CommentsApiImpl implements CommentsApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 获取动态评论
     *
     * @param movementId
     * @param comment
     * @param page
     * @param pagesize
     * @return java.util.List<com.tanhua.model.mongoDb.Comment>
     * @Author Pan Rundong
     */
    @Override
    public List<Comment> fingById(String movementId, CommentType comment, Integer page, Integer pagesize) {
        //1.创建对象
        Criteria criteria = Criteria.where("publishId").is(new ObjectId(movementId))
                .and("commentType").is(comment.getType());

        //2.设置条件，类型需要等于comment，以及分页
        Query query = Query.query(criteria).skip((page-1)*pagesize).limit(pagesize).
                with(Sort.by(Sort.Order.desc("created")));

        //3.查询
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        //4.返回
        return commentList;
    }

    /**
     * 发布评论
     *
     * @param comment
     * @return int
     * @Author Pan Rundong
     */
    @Override
    public int publish(Comment comment) {
        //1.查询动态
        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);

        //2.设置被评论人属性
        if (movement != null) {
            comment.setPublishUserId(movement.getUserId());
        }

        //3.保存
        mongoTemplate.save(comment);

        //4.更新字段
        Query query = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        if (comment.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount", 1);
        } else if (comment.getCommentType() == CommentType.LOVE.getType()) {
            update.inc("loveCount", 1);
        } else {
            update.inc("commentCount", 1);
        }

        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true) ;//获取更新后的最新数据
        Movement modify = mongoTemplate.findAndModify(query, update, options, Movement.class);
        //5、获取最新的评论数量，并返回
        return modify.statisCount(comment.getCommentType());
    }

    /**
     * 取消评论
     *
     * @param comment
     * @return int
     * @Author Pan Rundong
     */
    @Override
    public int delete(Comment comment) {
        //1.设置删除条件
        Criteria criteria = Criteria.where("userId").is(comment.getUserId())
                .and("publishId").is(comment.getPublishId())
                .and("commentType").is(comment.getCommentType());

        //2.删除评论信息
        Query queryDelete = Query.query(criteria);
        mongoTemplate.remove(queryDelete, Comment.class);

        //3.更新字段
        Query queryUpdate = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        if (comment.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount", -1);
        } else if (comment.getCommentType() == CommentType.LOVE.getType()) {
            update.inc("loveCount", -1);
        } else {
            update.inc("commentCount", -1);
        }

        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true) ;//获取更新后的最新数据
        Movement modify = mongoTemplate.findAndModify(queryUpdate, update, options, Movement.class);
        //4、获取最新的评论数量，并返回
        return modify.statisCount(comment.getCommentType());
    }
}
